package es.uclm.itsue.SQLite;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class AdapterActivity extends ListActivity {

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        BibliotecaSQLiteHelper bibHelper = new BibliotecaSQLiteHelper(this, "biblioteca.s3db", null, 2);
        SQLiteDatabase db = bibHelper.getReadableDatabase();
        
        Cursor c = db.rawQuery(" SELECT * FROM libros ", null);
        startManagingCursor(c);
        
        ListAdapter adapter = new SimpleCursorAdapter(this,
        		R.layout.database_item, c, 
        		new String[] {"libros.titulo", "libros.autor"}, 
        		new int[] {R.id.textView_titulo, R.id.textView_autor});

        setListAdapter(adapter); 
        getListView().setTextFilterEnabled(true);
        
    }
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Log.i(getPackageName(), "Pulsado item " + position);
		Toast.makeText(this, "Pulsado item " + position + " " +id, Toast.LENGTH_LONG).show();
	}
}
